﻿// CCompressDlg.cpp: 实现文件
//

#include "pch.h"
#include "HackSkill.h"
#include "CCompressDlg.h"
#include "afxdialogex.h"


// CCompressDlg 对话框

IMPLEMENT_DYNAMIC(CCompressDlg, CDialogEx)

CCompressDlg::CCompressDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CompressDlg, pParent)
{

}

CCompressDlg::~CCompressDlg()
{
	if (hDll) 
	{
		FreeLibrary(hDll);//释放已经加载的DLL模块
	}
}

void CCompressDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CCompressDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &CCompressDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON3, &CCompressDlg::OnBnClickedButton3)
	ON_WM_DROPFILES()
END_MESSAGE_MAP()


// CCompressDlg 消息处理程序


BOOL CCompressDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	ChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);
	ChangeWindowMessageFilter(0x0049, MSGFLT_ADD); // 0x0049 == WM_COPYGLOBALDATA


	hDll = LoadLibraryA("CompressDll.dll");//动态加载DLL模块句柄
	if (hDll)
	{
		MyCompressFunc = (pMyCompressFunc)GetProcAddress(hDll, "MyCompressFunc");//得到所加载DLL模块中函数的地址
		MyUnCompressFunc = (pMyUnCompressFunc)GetProcAddress(hDll, "MyUnCompressFunc");//得到所加载DLL模块中函数的地址
	}
	else
	{
		MessageBox(L"加载CompressDLL失败！",L"提示");
	}

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

//压缩
void CCompressDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	//获取文件路径
	CWnd* pWnd = GetDlgItem(IDC_MFCEDITBROWSE1);
	csPath = L"";
	pWnd->GetWindowTextW(csPath);
	if (L"" == csPath)
	{
		MessageBox(L"没有拖拽文件！", L"提示");
		return;
	}
	USES_CONVERSION;
	char* pPath = T2A(csPath);
	if (MyCompressFunc(pPath))
	{
		MessageBox(L"压缩成功！",L"提示");
	}
	else
	{
		MessageBox(L"压缩失败！", L"提示");
	}
}

//解压
void CCompressDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	//获取文件路径
	CWnd* pWnd = GetDlgItem(IDC_MFCEDITBROWSE1);
	csPath = L"";
	pWnd->GetWindowTextW(csPath);
	if (L"" == csPath)
	{
		MessageBox(L"没有拖拽文件！", L"提示");
		return;
	}
	USES_CONVERSION;
	char* pPath = T2A(csPath);
	char szFileName[10] = { 0 };
	memcpy_s(szFileName, 10, pPath + strlen(pPath) - 6, 6);
	if (!strcmp(szFileName, ".myzip"))
	{
		if (MyUnCompressFunc(pPath))
		{
			MessageBox(L"解压成功！", L"提示");
		}
		else
		{
			MessageBox(L"解压失败！", L"提示");
		}
	}
	else
	{
		MessageBox(L"不是正确的压缩文件！", L"提示");
	}
}


void CCompressDlg::OnDropFiles(HDROP hDropInfo)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	UINT count = 0;

	csPath = _T("");

	//从成功的拖放操作中检索文件的名称，获取拖拽文件的数目
	count = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);

	//如果只拖拽一个文件夹
	if (count == 1)
	{
		//获得拖拽的文件名
		DragQueryFile(hDropInfo, 0, filePath, sizeof(filePath));

		//保存文件路径
		csPath = filePath;

		//显示文件路径
		CWnd* pWnd = GetDlgItem(IDC_MFCEDITBROWSE1);
		pWnd->SetWindowText(filePath);
	}
	else if (count > 1)
	{
		MessageBox(_T("不支持多个文件！"), _T("提示"));
	}

	CDialogEx::OnDropFiles(hDropInfo);
}
